var body = document.getElementsByTagName('body')[0];
// 返回的错误信息
var error_404 = `
                <div class="middle-box text-center animated fadeInDown">
                    <h1>404</h1>
                    <h3 class="font-bold">页面未找到！</h3>

                    <div class="error-desc">
                        抱歉，页面好像去火星了~
                        <form class="form-inline m-t" role="form">
                            <button type="submit" class="btn btn-primary">请返回上一级页面</button>
                        </form>
                    </div>
                </div>
                `;
var error_500 = `
                <div class="middle-box text-center animated fadeInDown">
                    <h1>500</h1>
                    <h3 class="font-bold">服务器内部错误</h3>

                    <div class="error-desc">
                        服务器好像出错了，请检查您的网络连接...
                        <br/><a href="" class="btn btn-primary m-t">重新加载</a>
                    </div>
                </div>
                `;
var error_network = `
                    <div class="middle-box text-center animated fadeInDown">
                        <h3 class="font-bold">网络错误</h3>

                        <div class="error-desc">
                            检测到您的网络出现问题，请检查您的网络连接...
                            <br/><a href="" class="btn btn-primary m-t">重新加载</a>
                        </div>
                    </div>
                    `
// 封装ajax,返回json格式的数据
var async = function (url,type,data,onSuccess) {
    $.ajax({
        url:url,
        timeout:10000,
        type:type,
        dataType:'json',
        data:data,
        success : function (data) {
            if (onSuccess) {
                var data = JSON.parse(data);
                console.log(data);
                onSuccess(data);
            }
        },
        error : function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 500) {
                body.innerHTML = error_500;
                return ;
            }
            if (XMLHttpRequest.status == 404) {
                body.innerHTML = error_404;
                return ;
            }
        },
        complete : function (XMLHttpRequest,status) {      //请求完成后最终执行参数
            if (status == 'timeout') {                      //超时,status还有success,error等值的情况
                body.innerHTML = error_network;
                return ;
            }          
        }
    })
}
// 动态生成下拉框
var create_options = function (element,data) {
    for (var i = 0; i < data.length; i++) {
        var option  =  document.createElement("option");   
		var text  =  document.createTextNode(data[i].name);   
		option.appendChild(text);   
        option.value = data[i].id;
        element.appendChild(option);  
    }
    return element;
}
